﻿using System;
using System.Collections.Generic;
using System.Web;


namespace CCPSIP.AddressBook
{
    /// <summary>CCPSIP_067：取得人員識別證資訊 API</summary>
    /// idHeader = 裝置 UUID 前八碼。iOS 請使用 [CCPCrypto getIdHeader] 呼叫。
    /// checkcode = 裝置限時驗證碼。iOS 請使用 [CCPCrypto getCheckcode] 呼叫。

    public class get_id_info : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest req = context.Request;
            HttpResponse resp = context.Response;
            resp.ContentType = "text/plain";
            resp.ContentEncoding = System.Text.Encoding.UTF8;
            DateTime reqTime = DateTime.Now;
            string resultStr = "";

            try
            {
                //【01】取得POST參數，驗證身分
                string idHeader = req.Form["idHeader"];
                string checkcode = req.Form["checkcode"];
                string Callee_PPS_Code = req.Form["Callee_PPS_Code"];

                //檢查參數
                SipUserInfo UserInfo = SipKit.GetUserInfo(idHeader, checkcode, req);
                if (UserInfo == null)
                    throw new Exception("Error_01: Cannot identify this device.");  //無法識別此裝置

                if (!SipKit.VarifyPpsCode(Callee_PPS_Code))
                    throw new Exception("Error_02: Unvalid pps code.");  //無效的PPS_Code



                //【02】搜尋人員識別證資料
                List<OracleAddressBookKit.PeopleDetailInfo> ReturnObject = new List<OracleAddressBookKit.PeopleDetailInfo>();
                bool IsCheckOtherPerson = !string.IsNullOrEmpty(Callee_PPS_Code);

                if (IsCheckOtherPerson)
                    ReturnObject = OracleAddressBookKit.GetPersonIdInfo(Callee_PPS_Code);
                else
                    ReturnObject = OracleAddressBookKit.GetPersonIdInfo(UserInfo.PPS_CODE);

                //【03】紀錄Log
                if (IsCheckOtherPerson)
                {
                    if (!string.IsNullOrEmpty(UserInfo.PPS_CODE))
                        OracleAddressBookKit.InsertCheckIdLog(UserInfo.PPS_CODE, Callee_PPS_Code);
                    else //非長春員工, 例如警衛, 1940開頭
                        OracleAddressBookKit.InsertCheckIdLog(UserInfo.SIP_Num, Callee_PPS_Code);
                }

                //【99】回傳結果
                resp.ContentType = "application/json";
                resp.Charset = null;
                fastJSON.JSON.Instance.Parameters.EnableAnonymousTypes = true;
                resultStr = fastJSON.JSON.Instance.ToJSON(ReturnObject);
            }
            catch (Exception ex)
            {
                resp.ContentType = "application/xml";
                resultStr = ChrisKit.Ex2Xml(ex, false);
            }
            finally
            {
                int contentLength = resp.ContentEncoding.GetByteCount(resultStr);
                resp.AppendHeader("Content-Length", contentLength.ToString());
                resp.Write(resultStr);
                resp.Flush();
                resp.Close();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}